package com.game.tank;

import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.SpawnData;
import com.almasb.fxgl.physics.CollisionHandler;
import com.almasb.fxgl.ui.FontType;
import com.game.tank.components.TankComponent;
import com.game.tank.entity.GameEntityFactory;
import com.game.tank.enums.Group;
import javafx.collections.ObservableList;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.transform.Transform;

import java.util.Map;

import static com.almasb.fxgl.dsl.FXGL.*;

/**
 * @author dylovedz
 */
public class TankApplication extends GameApplication {

    public static final String SCORE = "score";
    private Entity tank;

    @Override
    protected void initSettings(GameSettings gameSettings) {
        gameSettings.setWidth(800);
        gameSettings.setHeight(600);
        gameSettings.setTitle("tankGame");
        gameSettings.setVersion("0.1.0");
    }

    @Override
    protected void onPreInit() {
        getSettings().setGlobalMusicVolume(0.8);
        getSettings().setGlobalSoundVolume(0.8);
        loopBGM("bg.mp3");
    }

    @Override
    protected void initGame() {
        getGameWorld().addEntityFactory(new GameEntityFactory());
        setLevelFromMap("level1.tmx");
        tank = spawn("tank");
    }

    private void createEnemy(Integer num) {
        for (int i = 0; i < num; i++) {
            spawn("enemy");
        }
    }

    @Override
    protected void initInput() {
        onKey(KeyCode.UP, () -> tank.getComponent(TankComponent.class).moveUp());
        onKey(KeyCode.DOWN, () -> tank.getComponent(TankComponent.class).moveDown());
        onKey(KeyCode.LEFT, () -> tank.getComponent(TankComponent.class).moveLeft());
        onKey(KeyCode.RIGHT, () -> tank.getComponent(TankComponent.class).moveRight());
        onKey(KeyCode.SPACE, () -> tank.getComponent(TankComponent.class).shoot());
    }

    @Override
    protected void initPhysics() {
        getPhysicsWorld().addCollisionHandler(new CollisionHandler(Group.PLAY, Group.ENEMY) {
            @Override
            protected void onCollisionBegin(Entity play, Entity enemy) {
                play("boom.wav");
                enemy.removeFromWorld();
                createEnemy(1);
                inc(SCORE, 10);
            }
        });

        getPhysicsWorld().addCollisionHandler(new CollisionHandler(Group.BULLET, Group.ENEMY) {
            @Override
            protected void onCollisionBegin(Entity bullet, Entity enemy) {
                createEnemy(1);
                inc(SCORE, 10);
                spawn("boom", new SpawnData(enemy.getX(), enemy.getY()));
                bullet.removeFromWorld();
                enemy.removeFromWorld();
            }
        });
    }

    @Override
    protected void initGameVars(Map<String, Object> vars) {
        vars.put(SCORE, 0);
    }

    @Override
    protected void initUI() {
        Text scoreTxt = getUIFactoryService().newText(getip(SCORE).asString("分数: %d"));
        scoreTxt.setX(680);
        scoreTxt.setY(20);
        scoreTxt.setFill(Color.BLACK);
        addUINode(scoreTxt);
    }

    public static void main(String[] args) {
        launch(args);
    }
}